function [GG,RR,eu,SDX,flag,ACont,ALag,ALead,PSI] =mod_production_economy_SSrules(param,mspec)
 
    %MSPEC 1  Classical Monetary Model, 2 NK Model
    initss  = [];     ssvec   = [];
    
    % =========================================================================
    %%  PART I: Parameters and Positions
    bet         = param(1);  % discount factor
    alpha       = param(2);  % elasticity param production function
    N           = param(3);  % Steady state hours
    AD          = param(4);  % Average duration of long-term debt
    PI          = param(5);  % steady state inflation rate
    b           = param(6);  % real market value of debt to GDP
    kap         = param(7);  % slope of the NKPC. Matters only if mspec=2;
    phipiM      = param(8);  % Taylor rule response to regular inflation
    deltabM     = param(9);  % Debt response with surplus (sb-sb^F)
    phipiF      = param(10); % Taylor rule response to unfunded inflation
    deltabF     = param(11); % Debt response with surplus (Sb^F)
    phiy        = param(12); % Taylor rule response to output
    rhotau      = param(13); % AR(1) coeff transfer shocks
    STDtau      = param(14); % STD deviation transfer shocks
    

    rho=(1-1/AD)/bet; % decay parameter long term debt
    
    
    %%  PART II: Position of Variables
 
    YLog=1;   % output
    NLog=2;   % employment
    WrLog=3;  % real wage
    rLog=4;   % real interest rate
    RLog=5;   % nominal interest rate
    piLog=6;  % inflation rate
    pbLog=7;  % price of long term bond
    bLog=8;   % real market value of debt to output (s_b in the paper)
    tauLog=9; % surplus to output ratio
    
    %Counterfactual economy
    YcLog=10;  % output
    NcLog=11;  % employment
    WrcLog=12; % real wage
    rcLog=13;  % real interest rate
    RcLog=14;  % nominal interest rate
    picLog=15; % inflation rate
    pbcLog=16; % price of long term bond
    bcLog=17;  % real market value of debt to output (s_b in the paper)
    taucLog=18;% surplus to output ratio
    %Shocks
    shktauFLog   = 19;      % goverment policy shock to surplus Funded
    shktauULog   = 20;      % goverment policy shock to surplus Unfunded
  
    
         
    % Final Counts
    NVarY   = shktauULog;
    Neq     = shktauULog;
    
    %%  PART III: Position of shocks
 
        etauF  = 1;
        etauU  = 2;
        NShocks= 2;

        % Matrix of Standard Deviations
        SDX=eye(NShocks);
        SDX(etauF,etauF)          = STDtau;
        SDX(etauU,etauU)          = STDtau;
     
        
    %%  PART IV: Model Declaration
    ACont   = zeros(Neq,NVarY);
    ALead   = zeros(Neq,NVarY);
    ALag    = zeros(Neq,NVarY);
    PSI     = zeros(Neq,NShocks);
    flag=1;
    
  
    %%  PART V: Steady-State
    
r=1/bet;                      % real interest rate
R=r*PI;                       % nominal interest rate
Y=N^(1-alpha);                % output
Wr=(1-alpha)*N^(-alpha);      % real wage
phi=1/Y*Wr*(1-N);             % parameter governing utility of leisure
Pb=1/(R*PI-rho);              % price of long term bond
tau=((1+rho*Pb)/Pb/PI-1)*b;   % surplus to output ratio

    %%  PART VI: Equations
    %NOTE variables indexed at time t and t+1 are written on the LHS of the
    %equation and lagged variables to the right
    ii=1;
    
    %% 1) Euler equation
     ACont(ii,YLog)           = -1;
     if mspec==1
         ACont(ii,rLog)       = -1;
    elseif mspec==2
         ACont(ii,RLog)       = -1;
         ALead(ii,piLog)      =  1; 
     end
     ALead(ii,YLog)           =  1;
    ii=ii+1;
    
    %% 2) Labor supply
    %% 
    LaborSupplyEq; 
    ii=ii+1;
   
    if mspec==1 %Classical monetary model
        %% 3) Labor demand
        ACont(ii,WrLog)       = 1;
        ACont(ii,NLog)        = -alpha; 
        ii=ii+1;    
    elseif mspec==2 %NK model
        %% 3) NK Phillips curve
        ACont(ii,piLog)  =1;
        ACont(ii,WrLog)  =-kap; 
        ALead(ii,piLog)  =-bet;
        ii=ii+1;
    end
    
    %% 4) Production function
    ACont(ii,YLog)       = 1;
    ACont(ii,NLog)       = -(1-alpha); 
    ii=ii+1;
    
    %% 5) Real rate definition
    ACont(ii,RLog)       =  1;
    ACont(ii,rLog)       = -1;   
    ALead(ii,piLog)      = -1;
    ii=ii+1;
    
    %% 6 ) Evolution of Debt
    DebtEq;
    ii=ii+1;
    
    %% 7 ) Price of Long term debt Debt
    PbEq;
    ii=ii+1;
    
    %% 8) Fiscal Rule
    ACont(ii,tauLog)     = 1;
    ACont(ii,shktauFLog) =-1;
    ACont(ii,shktauULog) =-1;
    
    ALag(ii,bcLog)=deltabM-deltabF;
    ALag(ii,bLog) =deltabF;
    ii=ii+1;
    
    
    %% 9) Taylor rule
    ACont(ii,RLog)       = 1;
    ACont(ii,picLog)     = -phipiM+phipiF;
    ACont(ii,piLog)      = -phipiF;
    ACont(ii,YLog)       = phiy;
    ii=ii+1;
    
    %% COUNTERFACTUAL ECONOMY
    
     %% 10) Euler equation
     ACont(ii,YcLog)           = -1;
     if mspec==1
         ACont(ii,rcLog)       = -1;
     elseif mspec==2
         ACont(ii,RcLog)       = -1;
         ALead(ii,picLog)      =  1;
     end
     ALead(ii,YcLog)           =  1;
    ii=ii+1;
    %% 11) Labor supply
    LaborSupplyEq_c; 
    ii=ii+1;
    
    if mspec==1 %Classical monetary model
        %% 12) Labor demand
        ACont(ii,WrcLog)      = 1;
        ACont(ii,NcLog)       = -alpha; 
        ii=ii+1;    
    elseif mspec==2 %NK model
        %% 12) NK Phillips curve
        ACont(ii,picLog) =1;
        ACont(ii,WrcLog) =-kap; 
        ALead(ii,picLog) =-bet;
        ii=ii+1;
    end
    
    %% 13) Production function
    ACont(ii,YcLog)       = 1;
    ACont(ii,NcLog)       = -(1-alpha); 
    ii=ii+1;
    
    %% 14) Real rate definition
    ACont(ii,RcLog)       = 1;
    ACont(ii,rcLog)       =-1;   
    ALead(ii,picLog)      =-1;
    ii=ii+1;
    
    %% 15) Evolution of Debt
    DebtEq_c;
    ii=ii+1;
    
    %% 16 ) Price of Long term debt Debt
    PbEq_c;
    ii=ii+1;
    
     %% 17) Fiscal Rule
    ACont(ii,taucLog)   =1;
    ACont(ii,shktauFLog)=-1;
    
    ALag(ii,bcLog)      =deltabM;    
    ii=ii+1;
    
     %% 18) Taylor rule
    ACont(ii,RcLog)       = 1;
    ACont(ii,picLog)      = -phipiM;
    ACont(ii,YcLog)       = -phiy;
    ii=ii+1;
       
    
    %% Exogenous Processes
    %% 19) Fiscal shock (Funded)
    ACont(ii,shktauFLog)  = 1;
    PSI(ii,etauF)         = 1;
    ALag(ii,shktauFLog)   = rhotau;
    ii=ii+1;
    
     %% 20) Fiscal shock (Ununded)
    ACont(ii,shktauULog)  = 1;
    PSI(ii,etauU)         = 1;
    ALag(ii,shktauULog)   = rhotau;
  
    
    %%  PART VII: Solve Model
    % AMA Solver
    [GG,~,RR,~,~,~,~,eu]=amasolve(ALead,ACont,ALag,zeros(NVarY,1),PSI);
    

    if isequal(eu(:),[1;1])==false
        
      
        ZZ  = [];
        SDX = [];
        RR  = [];
        ssvec=[];
        ssnames={};
        namesstates={};
        namesshocks={};
        disp('Model has no solution')
        flag=0;
       
    else
      
end


